package com.atzxh.datastructure.sort;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 小红同学
 * @create 2022-10-17-13:36
 * @description
 */
@SuppressWarnings({"all"})
public class ShellSort {
    public static void main(String[] args) {
        //int[] arr = {8,9,1,7,2,3,5,4,6,0};
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 800000);
        }

        // 开始计时
        Date start = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_str = simpleDateFormat.format(start);
        System.out.println("开始时间：" + start_str);

        shellSort2(arr);

        // 计时结束
        Date end = new Date();
        String end_str = simpleDateFormat.format(end);
        System.out.println("结束时间：" + end_str);
    }

    public static void shellSort(int[] arr){

        int temp = 0;
        // 控制组数
        for (int gap = arr.length / 2; gap > 0  ; gap /= 2) {
            // 实现分组
            for (int i = gap; i < arr.length; i++) {
                // 每次从0元素开始比较，步长为 gap
                for (int j = i - gap; j >=0 ; j -= gap) {
                    if (arr[j] > arr[j + gap]) {
                        temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
        }
    }

    public static void shellSort2(int[] arr) {
        for (int gap = arr.length / 2; gap > 0 ; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                int j = i;
                int temp = arr[j];
                if (arr[j] < arr[j - gap]) {
                    while (j - gap >= 0 && temp < arr[j - gap]) {
                        arr[j] = arr[j - gap];
                        j -= gap;
                    }
                }
            }
        }
    }
}
